<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="告警类型" prop="alarmType">
        <el-select
          v-model="queryParams.alarmType"
          placeholder="请选择告警类型"
          clearable
          style="width: 240px"
          @change="handleAlarmTypeChange"
        >
          <el-option
            v-for="dict in dict.type.alarm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备ID"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道" prop="chanel">
        <el-input
          v-model="queryParams.chanel"
          placeholder="请输入通道"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.alarm_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="米标范围">
        <el-input
          v-model="queryParams.minMeterMark"
          placeholder="最小米标"
          clearable
          style="width: 110px"
        />
        <span style="margin: 0 5px">-</span>
        <el-input
          v-model="queryParams.maxMeterMark"
          placeholder="最大米标"
          clearable
          style="width: 110px"
        />
      </el-form-item>
      <!-- 修改为断纤位置区间查询 -->
      <el-form-item label="断纤位置范围">
        <el-input
          v-model="queryParams.minFiberBreakPosition"
          placeholder="最小位置"
          clearable
          style="width: 110px"
        />
        <span style="margin: 0 5px">-</span>
        <el-input
          v-model="queryParams.maxFiberBreakPosition"
          placeholder="最大位置"
          clearable
          style="width: 110px"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:record:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="告警id" align="center" prop="id" />
      <el-table-column label="告警类型" align="center" prop="alarmType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_type" :value="scope.row.alarmType"/>
        </template>
      </el-table-column>
      <el-table-column label="设备No" align="center" prop="deviceNo" />
      <el-table-column label="通道" align="center" prop="chanel" />
      <el-table-column v-if="(queryParams.alarmType == '' || queryParams.alarmType==3)" label="米标" align="center" prop="meterMark" />
      <el-table-column v-if="(queryParams.alarmType == '' || queryParams.alarmType==1)" label="断纤告警位置" align="center" prop="fiberBreakPosition" />
      <el-table-column v-if="(queryParams.alarmType == '' || queryParams.alarmType==2)" label="激光器温度" align="center" prop="laserTemperature" />
      <el-table-column v-if="(queryParams.alarmType == '' || queryParams.alarmType==3)" label="温度报警状态" align="center" prop="meterAlarmStatus" />
      <el-table-column v-if="(queryParams.alarmType == '' || queryParams.alarmType==3)" label="报警时刻温度" align="center" prop="meterAlarmTemperature" />
      <el-table-column v-if="(queryParams.alarmType == '' || queryParams.alarmType==3)" label="告警状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listAlarmRecord } from "@/api/project/record"

export default {
  name: "Record",
  dicts: ['alarm_type', 'alarm_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 告警记录表格数据
      alarmRecordList: [],
      // 日期范围 - 默认当前时间及前一周
      dateRange: this.getDefaultDateRange(),
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmType: '',
        deviceNo: undefined,
        chanel: undefined,
        status: undefined,
        minMeterMark: undefined,
        maxMeterMark: undefined,
        minFiberBreakPosition: undefined, // 新增断纤位置最小值
        maxFiberBreakPosition: undefined  // 新增断纤位置最大值
      }
    }
  },
  created() {
    // 从路由参数中获取查询条件
    const query = this.$route.query
    if (query && Object.keys(query).length > 0) {
      this.queryParams = {...this.queryParams, ...query}
    }
    this.getList()
  },
  methods: {
    /** 返回按钮操作 */
    handleClose() {
      const obj = {path: "/project/event"}
      this.$tab.closeOpenPage(obj)
    },
    // 告警类型变更处理
    handleAlarmTypeChange() {
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    // 获取默认日期范围（当前时间及前一周）
    getDefaultDateRange() {
      const end = new Date();
      const start = new Date();
      start.setDate(start.getDate() - 30);
      return [this.formatDate(start), this.formatDate(end)];
    },
    // 格式化日期为 YYYY-MM-DD
    formatDate(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 查询告警记录列表 */
    getList() {
      this.loading = true
      listAlarmRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.alarmRecordList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push('/project/record/detail/' + row.id)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/record/export', {
        ...this.queryParams
      }, `alarmRecord_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
